import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.inspection import permutation_importance
import matplotlib.pyplot as plt
import seaborn as sns

#解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False



# 数据加载与预处理
data = pd.read_csv("data/train.csv")
data.drop(columns=['EmployeeNumber', 'StandardHours', 'Over18'], inplace=True)

# 关键修复：处理分类特征
categorical_cols = data.select_dtypes(include=['object']).columns
for col in categorical_cols:
    le = LabelEncoder()
    data[col] = le.fit_transform(data[col])  # 将分类特征转换为数值型

# 分离特征和目标变量
X = data.drop('Attrition', axis=1)
y = data['Attrition']

# 训练随机森林
rf = RandomForestClassifier(n_estimators=100, random_state=42, oob_score=True)
rf.fit(X, y)

# 基尼重要性
gini_importance = pd.DataFrame({
    'Feature': X.columns,
    'Gini_Importance': rf.feature_importances_
}).sort_values('Gini_Importance', ascending=False)
print("基尼重要性排名：\n", gini_importance)

# 置换重要性
perm_importance = permutation_importance(
    rf, X, y, n_repeats=10, random_state=42
)
perm_df = pd.DataFrame({
    'Feature': X.columns,
    'Perm_Importance': perm_importance.importances_mean
}).sort_values('Perm_Importance', ascending=False)
print("\n置换重要性排名：\n", perm_df)

# 合并结果
result_df = pd.merge(gini_importance, perm_df, on='Feature')

# 可视化对比
plt.figure(figsize=(10, 6))
result_df.set_index('Feature').plot(kind='bar')
plt.title("特征相关性对比：基尼重要性与置换重要性")
plt.ylabel("重要性得分")
plt.tight_layout()
plt.show()
